<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" >
        <title>Page Title</title>
        <script src="ajax.js" charset="utf-8"></script>
        <script type="text/javascript">
            window.onload=function(){
                    var oTxtUser = document.getElementById("user")
                    var oTxtPass = document.getElementById("pass")
                    var oBtnReg  = document.getElementById("reg_btn")
                    var oBtnLogin= document.getElementById("login_btn")
                    
                    oBtnLogin.onclick=()=>{
                        ajax({
                            url:'/user',
                            data:{act:'login',user:oTxtUser.value,pass:oTxtPass.value},
                            type:'get',
                            success: (str)=>{
                                var json = eval('('+str+')')

                                if(json.ok){
                                    alert('登陆成功！');
                                }else{
                                    alert('登陆失败：'+json.msg)
                                }
                            },
                            error:()=>{
                                alert('通信失败');
                            }
                        })
                    }

                    oBtnReg.onclick=function (){
                        ajax({
                            url: '/user',
                            data: {act: 'reg', user: oTxtUser.value, pass: oTxtPass.value},
                            type: 'get',
                            success: function (str){
                                var json=eval('('+str+')');

                                if(json.ok){
                                    alert('注册成功！');
                                }else{
                                    alert('注册失败：'+json.msg)
                                }
                            },
                            error: function (){
                                alert('通信错误');
                            }
                        })
                    }
                }
        </script>
    </head>
    <body>
        用户：<input type="text" id="user"><br>
        密码：<input type="password" id="pass"><br>
        <input type="button" value="注册" id="reg_btn">
        <input type="button" value="登录" id="login_btn">
    </body>
</html>